<?php
// https://leetcode-cn.com/problems/validate-binary-search-tree/submissions/
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return Boolean
     */
    function isValidBST($root) {
        return $this->juge($root, null, null);
    }

    function juge($root, $min, $max)
    {
        if ($root === null) {
            return true;
        }
        if ($max !== null && $root->val >= $max) {
            return false;
        }
        if ($min !== null && $root->val <= $min) {
            return false;
        }
        return $this->juge($root->left, $min, $root->val) && $this->juge($root->right, $root->val, $max);
    }
}